import java.util.Stack;

/**
 * @ClassName LC150
 * @Author ykx
 * @Date 2024/3/29 14:38
 * @Version 1.0
 */
public class LC150 {
    public static void main(String[] args) {
        LC150 lc150 = new LC150();
        System.out.println(lc150.evalRPN(new String[]{"4", "13", "5", "/", "+"}));
    }

    public int evalRPN(String[] tokens) {
        // 结果
        int result = 0;

        // 声明一个栈
        Stack<Integer> stack = new Stack<>();

        // 遍历tokens
        for (int i = 0; i < tokens.length; i++) {
            if (!"+-*/".contains(tokens[i])) {
                // 0-9分别入栈
                stack.add(Integer.parseInt(tokens[i]));
            } else {
                Integer a = stack.pop();
                Integer b = stack.pop();
                // 遇到加减乘除
                switch (tokens[i]) {
                    case "+":
                        // 加法
                        stack.add(a + b);
                        break;
                    case "-":
                        // 减法
                        stack.add(b - a);
                        break;
                    case "*":
                        // 乘法
                        stack.add(a * b);
                        break;
                    case "/":
                        // 除法
                        stack.add(b / a);
                        break;
                    default:
                        break;
                }
            }

        }

        return stack.pop();
    }
}
